﻿#include "FITKAbaqusHDF5AdaptorBeamOrientation.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaBeamOrientation.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorBeamOrientation::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorBeamOrientation";
    }

    bool FITKAbaqusHDF5AdaptorBeamOrientation::adaptR()
    {
        auto wire = dynamic_cast<Interface::FITKAbaBeamOrientation*>(_dataObj);
        if (!_reader || !wire || !_h5Group) return false;
        bool isR = true;
        isR &= readBeamOrientation(wire, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorBeamOrientation::adaptW()
    {
        auto wire = dynamic_cast<Interface::FITKAbaBeamOrientation*>(_dataObj);
        if (!_writer || !wire || !_h5Group) return false;
        bool isW = true;
        isW &= writeBeamOrientation(wire, *_h5Group);
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorBeamOrientation::readBeamOrientation(Interface::FITKAbaBeamOrientation * beamO, H5::Group & h5Group)
    {
        if (!beamO) return false;
        if (!readDataObject(beamO, h5Group)) return false;

        int ModelSetID = readIntAttribute(h5Group, "ModelSetID");
        beamO->setModelSetID(ModelSetID);
        QList<double> N1Direction;
        std::array<double, 3> stdN1Direction;
        if (!readDoubleAttrbuteDataSet(h5Group, "N1Direction", N1Direction, 1, 3)) return false;
        if (N1Direction.size() != 3) return false;
        stdN1Direction[0] = N1Direction[0];
        stdN1Direction[1] = N1Direction[1];
        stdN1Direction[2] = N1Direction[2];
        beamO->setN1Direction(stdN1Direction);
        return true;
    }

    bool FITKAbaqusHDF5AdaptorBeamOrientation::writeBeamOrientation(Interface::FITKAbaBeamOrientation * beamO, H5::Group & h5Group)
    {
        if (!beamO) return false;
        std::string groupName = createParentAttribute(beamO, h5Group);
        if (groupName.empty()) return false;
        auto beamOrientationGroup = h5Group.createGroup(groupName);

        int ModelSetID = beamO->getModelSetID();
        writeIntAttribute(beamOrientationGroup, "ModelSetID", &ModelSetID);

        auto N1Direction = beamO->getN1Direction();
        writeDoubleAttribute(beamOrientationGroup, "N1Direction", &N1Direction, 1, 3);
        return writeDataObject(beamO, beamOrientationGroup);
    }


}


